package serviceImpl;

import java.util.Calendar;
import java.util.List;
import javax.persistence.Query;
import service.DespesaService;
import model.Despesa;


public class DespesaServiceImpl extends GenericServiceImpl<Despesa> implements
		DespesaService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2829147401522567282L;
private Query query;

	

	@SuppressWarnings("unchecked")
	public List<Despesa> listDespesaEntreDatas(Integer mes,Integer ano, Long codigo) {
		Calendar cFim = Calendar.getInstance();
		Calendar cInicio = Calendar.getInstance();
		cInicio.set(Calendar.DAY_OF_MONTH, 1);
		cInicio.set(Calendar.MONTH, mes-1);
		cInicio.set(Calendar.YEAR, ano);
		cFim.set(Calendar.DAY_OF_MONTH, 1);
		cFim.set(Calendar.MONTH, mes-1);
		cFim.set(Calendar.YEAR, ano);
		cFim.set(Calendar.DAY_OF_MONTH, cFim.getActualMaximum(Calendar.DAY_OF_MONTH));
		cInicio.set(Calendar.DAY_OF_MONTH, cInicio.getActualMinimum(Calendar.DAY_OF_MONTH));

		query = getEm().createQuery(
				"Select c from Despesa c where c.usuario.codigo = :codigo and c.data between :inicio and :fim order by c.data");
		query.setParameter("codigo", codigo);
		query.setParameter("inicio", cInicio.getTime());
		query.setParameter("fim", cFim.getTime());
		return query.getResultList();
	}

}
